import React, { Component, PropTypes } from 'react';

import '../BodyDataInput.scss';
import LayoutView, { LayoutMode } from '../../layout';
import { SelectButtonGroup, SelectButton, SelectItemList, SelectItem } from '../../common-ui-parts';
import { BodyItemConstant as BiC } from '../../../constants/InputItemConstants';
import { BodyDataInputContextConstant } from '../../../constants/ComponentContextConstants';
import { MultipleValidationCategory as MvC } from '../../../validations/BodyDataInputValidation';

// 質問に関するイラスト
import imgQ1 from './images/imgQ1.png';

/**
 * Q1.基本情報
 */
class Question01 extends Component {
  getChildContext() {
    const { [BiC.GENDER]: gender, [BiC.AGE]: age, [BiC.BODY_HEIGHT]: height, [BiC.BODY_WEIGHT]: weight } = this.context.parent.inputData;
    return Object.freeze({
      parent: Object.freeze(Object.assign({}, this.context.parent, {
        // 未入力項目がある場合、[次へ]ボタンは無効
        disabledNext: [gender, age, height, weight].some(input => input === undefined),
      })),
    });
  }

  shouldComponentUpdate(nextProps, nextState) {
    return this.context.parent.pageIndex === nextProps.index;
  }

  scrollPageTop() {
    this.refs.layoutView.getWrappedInstance().scrollTop();
  }

  validatePage() {
    // 入力チェック（true:チェックOK）
    return this.context.parent.validateQuestionPage([BiC.GENDER, BiC.AGE, BiC.BODY_HEIGHT, BiC.BODY_WEIGHT], [MvC.BMI]);
  }

  render() {
    const { inputData, errorInfo, updateState } = this.context.parent;
    return (
      <LayoutView
        ref="layoutView"
        mode={LayoutMode.QUESTION}
        title="基本情報"
        backToHome={true}
        question={{
          no: 1,
          sentence: <p>性別・年齢などについて教えてください。<br/><br/></p>,
          illustSrc: imgQ1,
          illustHeight: 236,
        }}
        errorText={errorInfo.get(BiC.GENDER, BiC.AGE, BiC.BODY_HEIGHT, BiC.BODY_WEIGHT, MvC.BMI)}
      >
        {/* 性別 */}
        <dl className="content">
          <dt>{BiC.GENDER.label}</dt>
          <dd>
            <SelectButtonGroup
              name={BiC.GENDER}
              value={inputData[BiC.GENDER]}
              propagate={updateState}
            >
              {BiC.GENDER.choices.map((choice, index) => (
                <SelectButton key={index} value={choice.value} label={choice.label} />
              ))}
            </SelectButtonGroup>
          </dd>
        </dl>
        {/* 年齢 */}
        <dl className="content">
          <dt>{BiC.AGE.label}</dt>
          <dd>
            <SelectItemList
              name={BiC.AGE}
              value={inputData[BiC.AGE]}
              placeholder="選択してください"
              propagate={updateState}
            >
              {BiC.AGE.choices.map((choice, index) => (
                <SelectItem key={index} value={choice.value} label={choice.label} />
              ))}
            </SelectItemList>
          </dd>
        </dl>
        {/* 身長 */}
        <dl className="content">
          <dt>{BiC.BODY_HEIGHT.label}</dt>
          <dd>
            <SelectItemList
              name={BiC.BODY_HEIGHT}
              value={inputData[BiC.BODY_HEIGHT]}
              placeholder="選択してください"
              propagate={updateState}
            >
              {BiC.BODY_HEIGHT.choices.map((choice, index) => (
                <SelectItem key={index} value={choice.value} label={choice.label} />
              ))}
            </SelectItemList>
          </dd>
        </dl>
        {/* 体重 */}
        <dl className="content">
          <dt>{BiC.BODY_WEIGHT.label}</dt>
          <dd>
            <SelectItemList
              name={BiC.BODY_WEIGHT}
              value={inputData[BiC.BODY_WEIGHT]}
              placeholder="選択してください"
              propagate={updateState}
            >
              {BiC.BODY_WEIGHT.choices.map((choice, index) => (
                <SelectItem key={index} value={choice.value} label={choice.label} />
              ))}
            </SelectItemList>
          </dd>
        </dl>
      </LayoutView>
    );
  }

}

Question01.contextTypes = BodyDataInputContextConstant;
Question01.childContextTypes = Question01.contextTypes;

/*Question01.propTypes = {
  index: PropTypes.number,    // 自ページINDEX（0～）
};*/

export default Question01
